package com.yuoj.yuojbackendgateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

import java.util.Arrays;

/**
 * @author 刘渠好
 * @since 2025-04-13 21:25
 * 跨域配置
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration ();
        config.addAllowedMethod ("*");
        config.setAllowCredentials (true); //cookie
        config.setAllowedOriginPatterns (Arrays.asList("*"));  //支持跨域
        config.addAllowedHeader ("*"); //允许请求头信息
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);// 添加地址映射
        return new CorsWebFilter (source);
    }
}
